package com.yugao.fintech.antelope.auth.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yugao.fintech.antelope.auth.model.AuthLoginLog;
import com.yugao.fintech.antelope.auth.model.rqrs.log.LoginLogPageReq;
import com.yugao.fintech.antelope.auth.service.AuthLoginLogService;
import com.yugao.fintech.antelope.base.utils.ExcelContentType;
import com.yugao.fintech.antelope.base.utils.ExcelUtils;
import com.yugao.fintech.antelope.base.security.annotation.InnerAuth;
import com.yugao.fintech.antelope.base.log.LogTag;
import com.yugao.fintech.antelope.base.log.OperateLog;
import com.yugao.fintech.antelope.base.log.enums.BusinessType;
import com.yugao.fintech.antelope.base.ram.ActionType;
import com.yugao.fintech.antelope.base.ram.RamResource;
import com.yugao.fintech.framework.core.response.Resp;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 系统访问记录
 */
@LogTag(name = "登录日志")
@RestController
@RequestMapping("/loginLog")
public class LoginLogController {
    @Autowired
    private AuthLoginLogService loginLogService;

    @GetMapping("/page")
    @RamResource(action = "auth:loginLog:query", name = "查询登录日志", type = ActionType.READ)
    public Resp<IPage<AuthLoginLog>> page(LoginLogPageReq req) {
        IPage<AuthLoginLog> page = loginLogService.pageLoginLog(req);
        return Resp.ok(page);
    }

    @OperateLog(summary = "导出登录日志", businessType = BusinessType.EXPORT)
    @RamResource(action = "auth:loginLog:export", name = "导出登录日志", type = ActionType.READ)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LoginLogPageReq req) throws Exception {
        List<AuthLoginLog> list = loginLogService.list(req);
        ExcelUtils.exportExcel(response, ExcelContentType.XLS, "登录日志.xls", AuthLoginLog.class, list);
    }


    @RamResource(action = "auth:loginLog:delete", name = "删除登录日志", type = ActionType.WRITE)
    @OperateLog(summary = "删除登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteBatch")
    public Resp<?> deleteBatch(@RequestParam(value = "ids") Long[] loginLogIds) {
        return loginLogService.deleteBatch(loginLogIds) ? Resp.ok() : Resp.fail("删除失败");
    }


    @RamResource(action = "auth:loginLog:delete", name = "删除登录日志", type = ActionType.WRITE)
    @OperateLog(summary = "删除登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete")
    public Resp<?> delete(@RequestParam(value = "id") Long loginLogId) {
        return loginLogService.deleteBatch(new Long[] { loginLogId }) ? Resp.ok() : Resp.fail("删除失败");
    }

    @RamResource(action = "auth:loginLog:clean", name = "清除登录日志", type = ActionType.WRITE)
    @OperateLog(summary = "清空日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public Resp<?> clean() {
        loginLogService.clear();
        return Resp.ok();
    }

    /**
     * 删除多少天之前的日志
     */
    @InnerAuth
    @OperateLog
    @PostMapping("/deleteOfDayBefore")
    @RamResource(action = "sys:loginLog:delete", name = "删除登录日志", type = ActionType.WRITE)
    public Resp<?> deleteOfDayBefore(@RequestParam("daysBefore") Integer daysBefore) {
        loginLogService.deleteOfDaysBefore(daysBefore);
        return Resp.ok();
    }
}